import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demonstrate {

	public static Object[] makeArray() {

		Object[] objectArray = { new GoodClass(), new GoodClass(),
				new GoodClass(), new BadClass(), new GoodClass() };
		return objectArray;
	}

	public static void main(String[] args) {

		Object[] array = makeArray();
		String bad = array[0].getClass().getName();
		String good = array[1].getClass().getName();
		for (int i = 1; i < array.length-1; i++) {
			if (good.equals(bad)) {
				if (!(bad.equals(array[i + 1].getClass().getName()))) {
					bad = array[i + 1].getClass().getName();
				}
			} else if (!(good.equals(bad))) {
				if (!(good.equals(array[i - 1].getClass().getName()))) {
					bad = good;
				}
			}
		}
		System.out.println(bad);
		Method action = null;
		for (int i = 0; i < array.length; i++) {
			try {
				action = array[i].getClass().getMethod("goAway",
						(Class<?>[]) null);
			} catch (NoSuchMethodException e) {
				System.out.println();
				System.out.println("No such method.");
			} finally {
				if (action != null) {
					System.out.println();
					try {
						action = array[i].getClass().getMethod("goAway",
								(Class<?>[]) null);
						action.invoke(array[i], (Object[]) null);
						System.out.println("In class: "
								+ array[i].getClass().getName());
					} catch (NoSuchMethodException | InvocationTargetException
							| IllegalAccessException x) {
						System.out.println("No such method.");
					} finally {
						action = null;
					}
				} else {
					for (Field field : array[i].getClass().getDeclaredFields()) {
						field.setAccessible(true);
						try {
							System.out.println(field.getGenericType() + " "
									+ field.getName() + " "
									+ field.get(array[i]));
						} catch (IllegalArgumentException
								| IllegalAccessException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
	}
}
